/****************************************
*  This file created using "Writer B"   *
*      a program written by CTonk       *
*          ctonk87@hotmail.com          *
*     [timestamp 02/20/06  12:50:06]    *
****************************************/

// Room.cpp

#ifndef CPP_ROOM
#define CPP_ROOM

#include "definitions.h"
#include <iostream>
#include <fstream>
#include <list>
#include "room.h"
#include "object.h"
#include "reset.h"

int Room::numRooms = 0;

// Constructors...
Room::Room()
{
	_vnum = 0;
	_area = 0;
	_sector = 0;
	_flags = 0;
	_name = new char[50];
	for ( int x=0; x < 50; x++ )
		_name[x] = '\0';
	_descolor = new char[3];
	strcpy( _descolor, "{x" );
	_exit = new Exit[6];
	_noexit = true;
	numRooms++;
}

Room::Room( Room& it )
{
	_vnum = it._vnum;
	_area = it._area;
	_sector = it._sector;
	_flags = it._flags;
	for ( int x=0; x < 50; x++ )
		_name[x] = it._name[x];
	_desc = it._desc;
	_descolor[1] = it._descolor[1];
	for ( int x=0; x < 6; x++ )
		_exit[x] = it._exit[x];
	_noexit = it.NoExits();
	numRooms++;
}

Room::~Room()
{
	_area = 0;
	delete[] _name;
	delete[] _exit;
	numRooms--;
}

// Operators...
Room& Room::operator = ( Room& it )
{
	_vnum = it._vnum;
	_area = it._area;
	_sector = it._sector;
	_flags = it._flags;
	for ( int x=0; x < 50; x++ )
		_name[x] = it._name[x];
	_desc = it._desc;
	_descolor[1] = it._descolor[1];
	for ( int x=0; x < 6; x++ )
		_exit[x] = it._exit[x];
	_noexit = it.NoExits();
	return *this;
}

// Public accessor methods...
int Room::GetVnum()
{
	return _vnum;
}

void Room::SetVnum( int vnum )
{
	_vnum = vnum;
}

Area* Room::getArea() {
	return _area;
}

void Room::setArea( Area* area ) {
	_area = area;
}

int Room::GetSector()
{
	return _sector;
}

void Room::SetSector( int sector )
{
	_sector = sector;
	switch ( _sector ) {
		case  0: _descolor[1] = 'x'; break;
		case  1: _descolor[1] = 'x'; break;
		case  2: _descolor[1] = 'x'; break;
		case  3: _descolor[1] = 'x'; break;
		case  4: _descolor[1] = 'x'; break;
		case  5: _descolor[1] = 'x'; break;
		case  6: _descolor[1] = 'x'; break;
		case  7: _descolor[1] = 'x'; break;
		case  8: _descolor[1] = 'x'; break;
		case  9: _descolor[1] = 'x'; break;
		case 10: _descolor[1] = 'x'; break;
		default: _descolor[1] = 'x'; break;
	}
	return;
}

int Room::getFlags() {
	return _flags;
}

void Room::setFlags( int flags ) {
	_flags = flags;
}

char* Room::GetName( void )
{
	return _name;
}

char Room::GetName( int x )
{
	return _name[x];
}

void Room::SetName( char* val )
{
	strcpy( _name, val );
	return;
}

void Room::SetName( const char* val )
{
	strcpy( _name, val );
	return;
}

void Room::SetName( int x, char val )
{
	_name[x] = val;
}

std::string& Room::getDesc() {
	return _desc;
}

void Room::setDesc( std::string desc ) {
	_desc = desc;
}

char* Room::GetDescolor()
{
	return _descolor;
}

Exit* Room::GetExit( void )
{
	return _exit;
}

Exit& Room::GetExit( int x )
{
	return _exit[x];
}

void Room::SetNoexit( bool noexit )
{
	_noexit = noexit;

	return;
}

bool Room::NoExits()
{
	return this->_noexit;
}

std::list< Character* >& Room::GetClist() {
	return this->_cList;
}

void Room::Add( Character* c ) {
	this->_cList.push_back( c );
	return;
}

void Room::Remove( Character* c ) {
	this->_cList.remove( c );
	return;
}

bool Room::There( Character* c ) {
	for ( std::list< Character* >::iterator it = this->_cList.begin(); it != this->_cList.end(); it++ )
		if ( *(it) == c )
			return true;
	return false;
}

Character* Room::GetC( int i ) {
	std::list< Character* >::iterator it;
	int x = 0;

	for ( it = _cList.begin(); it != _cList.end(); it++, x++ )
		if ( x == i )
			return *it;

	return NULL;
}

std::list< Mob* >& Room::getMlist() {
	return _mList;
}

void Room::Add( Mob* m ) {
	_mList.push_back( m );
}

void Room::Remove( Mob* m ) {
	_mList.remove( m );
}

bool Room::There( Mob* m ) {
	std::list< Mob* >::iterator it;

	for ( it = _mList.begin(); it != _mList.end(); it++ )
		if ( (*it) == m )
			return true;

	return false;
}

Mob* Room::getM( int i ) {
	std::list< Mob* >::iterator it;
	int x = 0;

	for ( it = _mList.begin(); it != _mList.end(); it++, x++ )
		if ( x == i )
			return *it;

	return NULL;
}

std::list< Object* >& Room::getOlist() {
	return _oList;
}

void Room::Add( Object* obj ) {
	_oList.push_back( obj );
	return;
}

void Room::Remove( Object* obj ) {
	_oList.remove( obj );
	return;
}

bool Room::There( Object* obj ) {
	std::list< Object* >::iterator it;

	for ( it = _oList.begin(); it != _oList.end(); it++ )
		if ( (*it) == obj )
			return true;

	return false;
}

Object* Room::getO( int i ) {
	std::list< Object* >::iterator it;
	int x = 0;

	for ( it = _oList.begin(); it != _oList.end(); it++, x++ )
		if ( x == i )
			return *it;

	return NULL;
}

// Other methods...


void Room::Save( std::ofstream& save ) {
	save << "\n**ROOM\n";
	save << _vnum << ' ' << _sector << ' ' << _flags << '\n';
	save << _name << "\n";
	save << _desc << "~\n";
	for ( int x=0; x < 6; x++ )
		_exit[x].Save( save );
	save << '\n';
	return;
}

void Room::Load( std::ifstream& load ) {
	int sector = 0;
	char ctemp[MAX_INPUT_BUFFER];
	load >> _vnum >> sector >> _flags;
	SetSector( sector ); // Do it this way so the room description color (_descolor) is set automatically for us.
	load.get(); load.getline( _name, 500 );
	load.getline( ctemp, MAX_INPUT_BUFFER, '~' );
	_desc = ctemp;

	for ( int x=0; x < 6; x++ ) {
		this->_exit[x].Load( load );
		if ( this->_exit[x].GetThere() )
			this->SetNoexit( false );
	}

	return;
}

// Static methods...
const char* Room::StringSector( int i ) {
	return "sector";
}



#endif // #ifndef CPP_ROOM
